package com.ruoyi.management.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.management.pojo.param.AmmeterParam;
import com.ruoyi.management.pojo.param.QldbqyinfoParam;
import com.ruoyi.management.pojo.param.UserParam;
import com.ruoyi.management.pojo.vo.QLdbzymxVO;
import com.ruoyi.management.pojo.vo.QldbqyinfoVO;
import com.ruoyi.management.pojo.vo.UserResultVO;
import com.ruoyi.management.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import static com.ruoyi.common.core.domain.AjaxResult.error;
import static com.ruoyi.common.core.domain.AjaxResult.success;

@Slf4j
@RequestMapping("/users")
@RestController
@Api(tags = "租用管理接口")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private TokenService tokenService;

    @ApiOperation(value = "新增用户")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody UserParam userParam) {
        log.info("新增用户");
        boolean i = userService.add(userParam);
        if (i){
            return success("添加成功");
        }
        return error("添加失败");
    }

    @ApiOperation(value = "更新用户信息")
    @PostMapping("/update")
    public AjaxResult update(@RequestBody UserParam userParam) {
        log.info("更新用户信息");
        boolean i = userService.update(userParam);
        if (i){
            return success("更新成功");
        }
        return error("更新失败");
    }

    @ApiOperation(value = "关联用户的联系人列表")
    @PostMapping("/selectContact")
    public AjaxResult selectContact(@RequestBody UserParam userParam, HttpServletRequest request) {
        log.info("关联用户的联系人列表");
        LoginUser loginUser = tokenService.getLoginUser(request);
        if (ObjectUtils.isNotEmpty(loginUser) && ObjectUtils.isNotEmpty(loginUser.getUser())) {
            userParam.setNickName(loginUser.getUser().getNickName());
        }
        List<UserResultVO> userResultVOList = userService.selectContact(userParam);
        return success("查询成功",userResultVOList);
    }

    @ApiOperation(value = "租用中客户信息列表")
    @PostMapping("/selectUseList")
    public AjaxResult selectUseList(@RequestBody AmmeterParam ammeterParam) {
        log.info("租用中客户信息列表");
        List<QLdbzymxVO> userResultVOList = userService.selectUseList(ammeterParam);
        if (userResultVOList==null){
            return success("没有相关数据");
        }
        PageInfo<QLdbzymxVO> list = new PageInfo<>(userResultVOList);
        return success(list);
    }

    @ApiOperation(value = "退租客户信息列表")
    @PostMapping("/selectFreeList")
    public AjaxResult selectFreeList(@RequestBody AmmeterParam ammeterParam) {
        log.info("租用中客户信息列表");
        List<QLdbzymxVO> userResultVOList = userService.selectFileList(ammeterParam);
        if (userResultVOList==null){
            return success("没有相关数据");
        }
        PageInfo<QLdbzymxVO> list = new PageInfo<>(userResultVOList);
        return success(list);
    }

    @ApiOperation(value = "显示租户基础信息")
    @PostMapping("/selectzh")
    public AjaxResult selectzh(@RequestBody AmmeterParam ammeterParam) {
        log.info("显示租户基础信息");
        QldbqyinfoVO userResultVOList = userService.selectzh(ammeterParam);
        return success(userResultVOList);
    }

    @ApiOperation(value = "开票信息补充")
    @PostMapping("/addInvoice")
    public AjaxResult updateInvoice(@RequestBody QldbqyinfoParam qldbqyinfoParam) {
        log.info("开票信息补充");
        boolean i = userService.updateInvoice(qldbqyinfoParam);
        if (i){
            return success("更新成功");
        }
        return error("更新失败");
    }

    @ApiOperation(value = "删除用户")
    @PostMapping("/deleteContact")
    public AjaxResult deleteContact(@RequestBody UserParam userParam) {
        log.info("删除用户");
        boolean i = userService.deleteContact(userParam);
        if (i){
            return success("操作成功");
        }
        return success("操作失败");
    }

    @ApiOperation(value = "显示租户列表")
    @GetMapping("/selectEnterpriseList")
    public AjaxResult selectEnterpriseList(
            @RequestParam(value = "qymc",required = false) String qymc,
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize,
            @RequestParam("tgszt") String tgszt
    ) {
        log.info("显示租户列表");
        List<QldbqyinfoVO> userResultVOList = userService.selectContact1(qymc, tgszt, pageNum, pageSize);
        if (userResultVOList==null){
            return success("没有相关数据");
        }
        PageInfo<QldbqyinfoVO> list = new PageInfo<>(userResultVOList);
        return success(list);
    }

    @ApiOperation(value = "租户开关闸")
    @GetMapping("/switchGate")
    public AjaxResult switchGate(
            @RequestParam("khbh") String khbh,
            @RequestParam("tNumber") Integer tNumber
    ) {
        log.info("租户开关闸");
        boolean i = userService.switchGate(khbh,tNumber);
        if (i){
            return success("操作成功");
        }
            return error("操作失败");
    }

}
